package com.chenjiagou.flight.controller;

import com.chenjiagou.flight.model.Airport;
import com.chenjiagou.flight.service.AirportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/airports")
public class AirportController {

    @Autowired
    private AirportService airportService;

    @GetMapping
    public ResponseEntity<List<String>> getAirports() {
        List<String> airports = airportService.getAllAirportNames();
        return ResponseEntity.ok(airports);
    }

    @PostMapping
    public ResponseEntity<Void> addAirport(@RequestBody Airport airport) {
        try {
            airportService.addAirport(airport);
            return ResponseEntity.created(null).build(); // 返回 HTTP 201 Created
        } catch (Exception e) {
            return ResponseEntity.badRequest().build(); // 返回 HTTP 400 Bad Request
        }
    }
}